package com.wydexcel.generate.properties.generate;

import com.wydexcel.generate.properties.ExcelAbstractSheetProperties;
import com.wydexcel.generate.properties.s.ExcelWorkPlaceProperties;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.*;

public class ExcelConfGenerateImpl {


    ExcelWorkPlaceProperties placeProperties;
    Map<String, ExcelGenerateImpl> generateImplMap = new HashMap<>();
    private final Workbook workbook;

    public ExcelConfGenerateImpl(ExcelWorkPlaceProperties placeProperties, Workbook workbook) {
        this.placeProperties = placeProperties;
        this.workbook = workbook;
        build();
    }


    public ExcelWorkPlaceProperties getPlaceProperties() {
        return placeProperties;
    }

 
    public boolean build() {
        //
        for (Map.Entry<String, ExcelAbstractSheetProperties> entry : placeProperties.getMap().entrySet()) {
            String k = entry.getKey();
            for (String sheetName : k.split(",")) {
                if (null != generateImplMap.get(sheetName)) {
                    continue;
                }
                ExcelAbstractSheetProperties conf = entry.getValue();
                conf.setSheetName(sheetName);
                ExcelGenerateImpl excelGenerate = new ExcelGenerateImpl(conf, workbook);
                generateImplMap.put(sheetName, excelGenerate);
            }
        }
        return true;
    }

    public ExcelGenerateImpl generate(String sheetName, Map<String, Object> map) {
        ExcelGenerateImpl excelGenerate = generateImplMap.get(sheetName);
        if (null == excelGenerate) {
            throw new IllegalArgumentException("");
        }
        excelGenerate.generate(map);

        return excelGenerate;
    }

    public ExcelGenerateImpl generate(String sheetName) {
        ExcelGenerateImpl excelGenerate = generateImplMap.get(sheetName);
        if (null == excelGenerate) {
            throw new IllegalArgumentException("");
        }
//        excelGenerate.generate(Collections.emptyMap());
        return excelGenerate;
    }

}

